package jsyf.oa.service.doc;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.doc.Main2RoleDao;
import jsyf.oa.entity.doc.Main2RoleEntity;
import jsyf.oa.entity.doc.TypeEntity;
import jsyf.oa.facade.doc.Main2RoleFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/20.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class Main2RoleService implements Main2RoleFacade {

    @Autowired
    private Main2RoleDao main2RoleDao;

    @Override
    public JSONObject getMainRoleList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String roleId = jsonParams.getString("roleId");
        String docMainId = jsonParams.getString("docMainId");
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;

        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();

        //condition.append(" ORDER BY roleId DESC");
        //查询list
        List<JSONObject> records = main2RoleDao.getJSONListByPage(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = main2RoleDao.getPageCount(condition.toString(), sqlArgs.toArray());
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }


    @Override
    public String save(String jsonData) {
        Main2RoleEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), Main2RoleEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = main2RoleDao.add(entity);
        } else {
            Main2RoleEntity main2RoleEntity = (Main2RoleEntity) main2RoleDao.getIBaseEntityById(entity.getId());
            main2RoleEntity.setDocMainId(entity.getDocMainId());
            main2RoleEntity.setRoleId(entity.getRoleId());
            rows = main2RoleDao.update(main2RoleEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public Boolean delById(String id) {
        return main2RoleDao.delById(id) > 0;
    }
}